<template>
  <div class="app-container">
    <el-card class="box-card" v-if="pageType == 1">
      <!-- 搜索区域 -->
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
        <el-form-item label="调拨单号" prop="transferOrderNumber">
          <el-input v-model="queryParams.transferOrderNumber" placeholder="请输入调拨单号" clearable size="small" />
        </el-form-item>
        <el-form-item label="调出门仓" prop="transferOutRoomWarehouseName">
          <el-cascader
            v-model="transferOutStoreId"
            :options="storeList"
            :props="{ expandTrigger: 'hover', value: 'id', label: 'name' }"
            @change="handleTransferOutStoreChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="调入仓库" prop="transferInRoomWarehouseName">
          <el-cascader
            v-model="transferInStoreId"
            :options="storeList"
            :props="{ expandTrigger: 'hover', value: 'id', label: 'name' }"
            @change="handleTransferInStoreChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="调拨状态" prop="transferType">
          <el-select v-model="queryParams.transferType" placeholder="请选择调拨状态" clearable size="small">
            <el-option v-for="item in transferTypeList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 操作按钮区域 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增调拨单</el-button>
        </el-col>
      </el-row>

      <!-- 数据表格 -->
      <el-table v-loading="loading" :data="tableData">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="调拨单号" align="center" prop="transferOrderNumber" />
        <el-table-column label="调拨类型" align="center" prop="transferType">
          <template slot-scope="scope">
            {{ getTransferType(scope.row.transferType) }}
          </template>
        </el-table-column>

        <el-table-column label="调出仓库" align="center" prop="transferOutRoomWarehouseName" />
        <el-table-column label="调入仓库" align="center" prop="transferInRoomWarehouseName" />
        <!-- 总数量 -->
        <el-table-column label="总数量" align="center" prop="totalNumber" />
        <!-- 总金额 -->
        <!-- <el-table-column label="总金额" align="center" prop="totalPrice" /> -->
        <!-- 创建人-->
        <el-table-column label="创建人" align="center" prop="creator" />
        <!-- 审核状态 -->
        <el-table-column label="审核状态" align="center" prop="reviewStatus">
          <template slot-scope="scope">
            {{ getTransferStatus(scope.row.reviewStatus) }}
          </template>
        </el-table-column>
        <!-- 创建时间 -->
        <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button> -->
            <el-button size="mini" type="text" @click="handleDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.limit"
        @pagination="getList"
      />
    </el-card>
    <el-card class="box-card" v-else-if="pageType == 3">
      <select-product
        @handleBack="handleBack"
        :defaultTransferInType="defaultTransferInType"
        :defaultTransferOutType="defaultTransferOutType"
      ></select-product>
    </el-card>
    <el-card class="box-card" v-else-if="pageType == 2">
      <!-- 详情 -->
      <warehouse-allocation-detail :detail-row="detailRow" :detail-data="detailData" @back="handleBack" />
    </el-card>
  </div>
</template>

<script>
import warehouseAllocationDetail from './detail.vue';
import { listStore } from '@/api/commercial';
import { storeHouseMixin } from '@/mixins/storeHouse';
import selectProduct from '@/views/storehouse/components/selectProduct.vue';
import pagination from '@/views/storehouse/components/pagination.vue';
import { getInventoryTransferList, getInventoryTransferInfo } from '@/api/storehouse';
export default {
  name: 'WarehouseAllocation',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 表格数据
      tableData: [
        {
          transferOrderNumber: '123456',
          transferOutRoomWarehouseName: '123456',
          transferInRoomWarehouseName: '123456',
          status: '123456',
          createTime: '123456',
        },
      ],

      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        transferType: '',
        transferOrderNumber: '',
      },
      defaultTransferInType: 1,
      defaultTransferOutType: 1,
    };
  },
  mixins: [storeHouseMixin],
  created() {
    this.getList();
  },
  components: {
    selectProduct,
    pagination,
    warehouseAllocationDetail,
  },
  methods: {
    handleTransferOutStoreChange(val) {
      this.transferOutType = val[0];
    },
    handleTransferInStoreChange(val) {
      this.transferInType = val[0];
    },

    /** 详情按钮操作 */
    handleDetail(row) {
      this.pageType = 2;
      this.detailRow = row;
      getInventoryTransferInfo({ id: row.id, page: 1, limit: 999 }).then((res) => {
        this.detailData = res.list;
      });
    },

    /** 查询列表 */
    getList() {
      this.loading = true;
      let params = {
        page: this.queryParams.page,
        limit: this.queryParams.limit,
        transferInRoomWarehouseId: this.transferInStoreId[1],
        transfeInType: this.transfeInType,
        transferOutRoomWarehouseId: this.transferOutStoreId[1],
        transferOutType: this.transferOutType,
        transferOrderNumber: this.queryParams.transferOrderNumber,
        transferType: this.queryParams.transferType,
      };
      getInventoryTransferList(params).then((res) => {
        this.tableData = res.list;
        this.total = res.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
  
    /** 新增按钮操作 */
    handleAdd() {
      // this.isAdd = true
      this.pageType = 3;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // TODO: 实现修改逻辑
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // TODO: 实现删除逻辑
    },
  },
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>
